package common;

import java.util.HashMap;

import javax.swing.JPanel;
import javax.swing.JProgressBar;

import org.w3c.dom.Element;

import fitness.Fitness;
import fitness.SquareFitness;

import optimizers.commons.RunConfig;
import optimizers.ganeat.Constants;
import optimizers.ganeat.GA;
import optimizers.ganeat.sin.ConstantsSin;
import optimizers.ganeat.square.GASquare;

public class RunSquareOscillation extends RunStarter {

	@Override
	public HashMap<String, String> getDefaultParameters() {
		HashMap<String, String> parameters = new HashMap<String, String>();
		parameters.put("Skip Time", String.valueOf(ConstantsSin.skipTime));
		parameters.put("Number of periods",
				String.valueOf(ConstantsSin.numberofperiods));
		parameters.put("Oscillation Period",
				String.valueOf(ConstantsSin.period));
		parameters.put("Oscillation Height",
				String.valueOf(ConstantsSin.height));
		parameters.put("Penalty", String.valueOf(ConstantsSin.penalty));
		return parameters;
	}

	@Override
	public void setParameters(HashMap<String, String> parameters) {
		try {
			ConstantsSin.skipTime = Integer.parseInt(parameters
					.get("Skip Time"));
			ConstantsSin.numberofperiods = Integer.parseInt(parameters
					.get("Number of periods"));
			ConstantsSin.period = Integer.parseInt(parameters
					.get("Oscillation Period"));
			ConstantsSin.height = Double.parseDouble(parameters
					.get("Oscillation Height"));
			ConstantsSin.penalty = Double
					.parseDouble(parameters.get("Penalty"));
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	@Override
	public String getName() {
		return "Square Oscillation";
	}

	@Override
	public void start() {
		runStatus.status = RunStatus.RUNNING;
		for (int i = 0; i < Constants.nRuns; i++) {
			GASquare ga = (GASquare) getGA();
			try {
				ga.start();
			} catch (CloneNotSupportedException | InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public Fitness getFitnessFunction() {
		// TODO Auto-generated method stub
		return new SquareFitness();
	}

	@Override
	public GA getGA() {
		// TODO Auto-generated method stub
		return new GASquare(config, initIndiv, panelFitness, panelSpecies,
				panelIndiv, runStatus, progressBar, fitnessElement);
	}

}
